package com.cal.utms.action.dealing;

import java.util.ArrayList;
import java.util.List;

import com.cal.utms.dao.dealing.DealDao;
import com.cal.utms.dao.dealing.InstrumentDao;
import com.cal.utms.dao.maintenance.InstrumentTypeDao;
import com.cal.utms.dto.dealing.InsIssueReceiveDto;
import com.cal.utms.entity.Deal;
import com.cal.utms.entity.FundInstrumentMap;
import com.cal.utms.entity.Instrument;
import com.cal.utms.entity.InstrumentType;
import com.cal.utms.model.dealing.InsIssueReceiveModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class InsIssueReceiveAction extends ActionSupport implements
ModelDriven<InsIssueReceiveDto> {
	
	InsIssueReceiveDto insIssueReceiveDto = new InsIssueReceiveDto(); 

	private static final long serialVersionUID = 1L;
	
	public String getDeals(){
		DealDao dealDao = new DealDao();
		insIssueReceiveDto.setDeals(dealDao.filterInitialDeals());
		return Action.SUCCESS;
	}
	
	public String getDeal(){
		DealDao dealDao = new DealDao();
		Deal deal = dealDao.getById(insIssueReceiveDto.getId());
		if(deal == null){
			insIssueReceiveDto.setSuccess(false);
			insIssueReceiveDto.setStatus("Deal Not Found");
		}else{
			deal.getFund().setFundManager(null);
			insIssueReceiveDto.setDeal(deal);
		}
		return Action.SUCCESS;
	}
	
	public String getFundInsTypeForDealType(){
		if(insIssueReceiveDto.getQueryOne()!=null){
			DealDao dealDao = new DealDao();
			Deal deal = dealDao.getByCode(insIssueReceiveDto.getQueryOne());
			if(deal!=null){
				List<InstrumentType> insTypes = new ArrayList<InstrumentType>();
				InstrumentTypeDao insTypeDao = new InstrumentTypeDao();
				InstrumentType insType= null;
				for(FundInstrumentMap fim : deal.getFund().getFundInstrumentMaps()){
					if(fim.getDealTypeBean().getCode().equals(insIssueReceiveDto.getQueryTwo())){
						insType = insTypeDao.getById(fim.getInstrumentTypeId());
						if(insType!=null){
							insTypes.add(insType);
						}
					}					
				}
				insIssueReceiveDto.setInsTypes(insTypes);
			}else{
				insIssueReceiveDto.setStatus("Deal Not Found");
				insIssueReceiveDto.setSuccess(false);
			}
		}else{
			insIssueReceiveDto.setStatus("Null Deal Code");
			insIssueReceiveDto.setSuccess(false);
		}
		return Action.SUCCESS;
	}
	
	public String getInstruments(){
		int fundId = 0;
		String dealCode = insIssueReceiveDto.getQueryOne();
		String dealTypeCode = insIssueReceiveDto.getQueryTwo();
		
		DealDao dealDao = new DealDao();
		Deal deal = dealDao.getByCode(dealCode);
		if(deal!=null){
			fundId = deal.getFund().getId();
			if(dealTypeCode!=null && dealTypeCode.equals("REPO")){
				InstrumentDao insDao = new InstrumentDao();
				List<Instrument> insList = insDao.getSalableInsCodes(fundId);
				if(insList!=null){
					insIssueReceiveDto.setInsList(insList);
				}else{
					insIssueReceiveDto.setInsList(new ArrayList<Instrument>());
				}			
			}else if(dealTypeCode!=null && dealTypeCode.equals("RREPO")){
				InstrumentDao insDao = new InstrumentDao();
				List<Instrument> insList = insDao.getFreeListed();
				if(insList!=null){
					insIssueReceiveDto.setInsList(insList);
				}else{
					insIssueReceiveDto.setInsList(new ArrayList<Instrument>());
				}	
			}
		}else{
			if(dealTypeCode!=null && dealTypeCode.equals("RREPO")){
				InstrumentDao insDao = new InstrumentDao();
				List<Instrument> insList = insDao.getFreeListed();
				if(insList!=null){
					insIssueReceiveDto.setInsList(insList);
				}else{
					insIssueReceiveDto.setInsList(new ArrayList<Instrument>());
				}	
			}else{
				System.out.println("Invalid deal code"+ dealCode);
				insIssueReceiveDto.setInsList(new ArrayList<Instrument>());
				insIssueReceiveDto.setStatus("Invalid Deal Code");
				insIssueReceiveDto.setSuccess(false);
			}			
		}
		return Action.SUCCESS;
	}
	
	public String updateInsTrans(){
		if(insIssueReceiveDto.getDealCode()==null){
			insIssueReceiveDto.setSuccess(false);
			insIssueReceiveDto.setStatus("Deal Code Null!");
			return Action.SUCCESS;
		}else if(insIssueReceiveDto.getTransType()==null){
			insIssueReceiveDto.setSuccess(false);
			insIssueReceiveDto.setStatus("Transaction Type is Null!");
			return Action.SUCCESS;
		}else{
			DealDao dealDao = new DealDao();
			Deal deal = dealDao.getByCode(insIssueReceiveDto.getDealCode());
			if(deal!=null){
				InsIssueReceiveModel insIssueReceiveModel = new InsIssueReceiveModel();
				if(insIssueReceiveDto.getTransType().equals("Withhold Certificate")){
					deal.setWithholdingCertificate(insIssueReceiveDto.getWhtCertStatus());
					dealDao.update(deal);					
				}else{
					insIssueReceiveModel.updateDIM(deal, insIssueReceiveDto);
				}
			}else{
				insIssueReceiveDto.setSuccess(false);
				insIssueReceiveDto.setStatus("Cannot Find Deal!");
				return Action.SUCCESS;				
			}			
		}
		System.out.println("DealCode:"+insIssueReceiveDto.getDealCode());
		return Action.SUCCESS;
	}

	@Override
	public InsIssueReceiveDto getModel() {
		return insIssueReceiveDto;
	}

}
